package com.andreamerla.petslife.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.*;
import com.andreamerla.petslife.R;
import com.andreamerla.petslife.manager.AlertDialogManager;
import com.andreamerla.petslife.util.Utils;

/**
 * Created by IntelliJ IDEA.
 * User: a.merla
 * Date: 15/07/13
 * Time: 17.37
 */
public class FoodDoseActivity extends ActionBarActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
    private EditText etWeight;
    private Spinner sAge;
    private Spinner sAgePuppy;
    private Spinner sSterilization;
    private Spinner sVitalityNotSterilized;
    private Spinner sVitalitySterilized;
    private Button btCalculate;
    private Button btClear;
    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.food_dose_layout);

        etWeight = (EditText) findViewById(R.id.et_weight);
        sAge = (Spinner) findViewById(R.id.s_age);
        sAge.setOnItemSelectedListener(this);
        sAgePuppy = (Spinner) findViewById(R.id.s_age_puppy);
        sAgePuppy.setOnItemSelectedListener(this);
        sSterilization = (Spinner) findViewById(R.id.s_sterilization);
        sSterilization.setOnItemSelectedListener(this);
        sVitalityNotSterilized = (Spinner) findViewById(R.id.s_vitality_not_sterilized);
        sVitalityNotSterilized.setOnItemSelectedListener(this);
        sVitalitySterilized = (Spinner) findViewById(R.id.s_vitality_sterilized);
        sVitalitySterilized.setOnItemSelectedListener(this);

        btCalculate = (Button) findViewById(R.id.bt_calculate);
        btCalculate.setOnClickListener(this);
        btClear = (Button) findViewById(R.id.bt_clear);
        btClear.setOnClickListener(this);

        tvResult = (TextView) findViewById(R.id.tv_result);
    }

    public void onClick(View view) {
        if (view == btCalculate) {
            calculate();
            Utils.hideKeyboard(this, view);
        } else if (view == btClear) {
            clear();
            Utils.hideKeyboard(this, view);
        }
    }

    private void calculate() {
        try {
            float weight = Float.parseFloat(etWeight.getText().toString());

            float kcalPerKg = 0;
            if (sAge.getSelectedItemPosition() == 1) {
                if (sAgePuppy.getSelectedItemPosition() == 0)
                    kcalPerKg = 250;
                else if (sAgePuppy.getSelectedItemPosition() == 1)
                    kcalPerKg = 130;
                else if (sAgePuppy.getSelectedItemPosition() == 2)
                    kcalPerKg = 100;
            } else if (sAge.getSelectedItemPosition() == 2) {
                if (sSterilization.getSelectedItemPosition() == 1) {
                    if (sVitalityNotSterilized.getSelectedItemPosition() == 0)
                        kcalPerKg = 50;
                    else if (sVitalityNotSterilized.getSelectedItemPosition() == 1)
                        kcalPerKg = 60;
                    else if (sVitalityNotSterilized.getSelectedItemPosition() == 2)
                        kcalPerKg = 70;
                    else if (sVitalityNotSterilized.getSelectedItemPosition() == 3)
                        kcalPerKg = 105;
                    else if (sVitalityNotSterilized.getSelectedItemPosition() == 4)
                        kcalPerKg = 250;
                } else if (sSterilization.getSelectedItemPosition() == 2) {
                    if (sVitalitySterilized.getSelectedItemPosition() == 0)
                        kcalPerKg = 45;
                    else if (sVitalitySterilized.getSelectedItemPosition() == 1)
                        kcalPerKg = 54;
                    else if (sVitalitySterilized.getSelectedItemPosition() == 2)
                        kcalPerKg = 63;
                }
            } else if (sAge.getSelectedItemPosition() == 3) {
                kcalPerKg = 40;
            }

            int kcalAlGiorno = (int) Utils.round(weight * kcalPerKg, 0);

            tvResult.setText(getString(R.string.my_cat_result, kcalAlGiorno));
            tvResult.setVisibility(View.VISIBLE);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            AlertDialogManager.showAlertDialog(this, getString(R.string.error), getString(R.string.error_invalid_data));
        }
    }

    private void clear() {
        etWeight.setText("");
        tvResult.setVisibility(View.GONE);

        etWeight.requestFocus();
    }

    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
        if (adapterView == sAge) {
            hideAll();

            switch (position) {
                case 0:
                    break;
                case 1:
                    sAgePuppy.setVisibility(View.VISIBLE);
                    onItemSelected(sAgePuppy, null, sAgePuppy.getSelectedItemPosition(), 0L);
                    break;
                case 2:
                    sSterilization.setVisibility(View.VISIBLE);
                    onItemSelected(sSterilization, null, sSterilization.getSelectedItemPosition(), 0L);
                    break;
                case 3:
                    btCalculate.setEnabled(true);
                    break;
            }
        } else if (adapterView == sAgePuppy) {
            btCalculate.setEnabled(true);
        } else if (adapterView == sSterilization) {
            hideAll();
            sSterilization.setVisibility(View.VISIBLE);

            switch (position) {
                case 0:
                    break;
                case 1:
                    sVitalityNotSterilized.setVisibility(View.VISIBLE);
                    onItemSelected(sVitalityNotSterilized, null, sVitalityNotSterilized.getSelectedItemPosition(), 0L);
                    break;
                case 2:
                    sVitalitySterilized.setVisibility(View.VISIBLE);
                    onItemSelected(sVitalitySterilized, null, sVitalitySterilized.getSelectedItemPosition(), 0L);
                    break;
            }
        } else if (adapterView == sVitalityNotSterilized) {
            btCalculate.setEnabled(true);
        } else if (adapterView == sVitalitySterilized) {
            btCalculate.setEnabled(true);
        }
    }

    public void onNothingSelected(AdapterView<?> adapterView) {
        // Do nothing
    }

    private void hideAll() {
        sAgePuppy.setVisibility(View.GONE);
        sSterilization.setVisibility(View.GONE);
        sVitalityNotSterilized.setVisibility(View.GONE);
        sVitalitySterilized.setVisibility(View.GONE);
        btCalculate.setEnabled(false);
    }
}
